﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.Web;
using System.IO;
using System.Collections;

namespace Core.DS
{
    public static partial class statistics
    {
        public static dynamic getDSStatistics(int eid, int cid)
        {
            var sql = new Sql.SqlDataContext();
            if (!sql.DSExperiment.Any(row => row.eid == eid)) return new
            {
                success = false,
                msg = "实验不存在"
            };
            if (!sql.DSClass.Any(row => row.cid == cid)) return new
            {
                success = false,
                msg = "班级不存在"
            };
            var ps = sql.DSProblem.Where(row => row.eid == eid).OrderBy(row => row.pid);
            var us = sql.DSClassStudent.Where(row => row.cid == cid).OrderBy(row => row.NO);
            var result = new Hashtable[us.Count()];
            int i = 0;
            foreach (var u in us)
            {
                var f = sql.DSExperimentSolve.FirstOrDefault(row => row.eid == eid && row.sid == u.sid);
                result[i] = new Hashtable
                {
                    {"no", u.NO},
                    {"sid", u.sid},
                    {"name",u.name},
                    {"sex", u.sex.toSex()},
                    {"accept", sql.DSSolve.Count(row=>row.eid==eid&&row.sid==u.sid&&row.accept>0)},
                    {"report", sql.DSExperimentSolve.Any(row => row.eid == eid && row.sid == u.sid)},
                    {"content", f == null ? null : f.reportContent},
                    {"point", f == null ? null : f.point}
                };
                foreach (var p in ps)
                {
                    var so = sql.DSSolve.FirstOrDefault(row => row.eid == eid && row.sid == u.sid && row.pid == p.pid && row.accept > 0);
                    var charpid = ((char)(65 + p.pid)).ToString();
                    result[i].Add(charpid, so != null ? charpid : "");
                    if (so != null)
                    {
                        result[i].Add(charpid + "_sim", so.similarityDegree);
                        result[i].Add(charpid + "_simrid", so.similarityRid);
                    }
                }
                ++i;
            }
            return new
            {
                success = true,
                result
            };
        }
    }
}
